package com.chis.cq;

import com.chis.jm.pojo.sys.TsZone;
import com.chis.jmdatatimer.comm.TxMgmtModule;
import com.chis.jmdatatimer.comm.TxMgmtModuleMysql;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年12月30日
 */
public class ZoneImport {
    public static void main(String[] args) {
        File file1 = new File("C:\\Users\\Administrator\\Desktop\\chongqing1.txt");
        FileReader m = null;
        try {
            m = new FileReader(file1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<TsZone> list = new ArrayList<>();
        BufferedReader reader = new BufferedReader(m);
        while (true) {
            String nextline = null;
            try {
                nextline = reader.readLine();
                if (nextline != null) {
//                    System.out.println(nextline);
                    String[] zones = nextline.split("#");
                    String zonegb = zones[0];
                    String zonename=zones[1];
                    TsZone tsZone = new TsZone();
                    StringBuilder sb = new StringBuilder();
                    sb.append("insert into TS_ZONE (");
                    sb.append("ZONE_GB,ZONE_NAME,SPLSHT,ZONE_LEV_CODE,ZONE_TYPE_CODE,ZONE_COUNGB,IF_REVEAL,DELMARK,CREATE_DATE,MODIFY_DATE) ");
                    sb.append("values (");
                    if(zonegb.length()==2){
                        tsZone.setZoneGb(Long.valueOf(zonegb+"00000000"));
                        tsZone.setZoneCoungb(Long.valueOf(zonegb+"00000000"));
                        tsZone.setZoneLevCode("1002");
                        sb.append("'").append(zonegb).append("00000000','").append(zonename).append("','").append(ToFirstChar(zonename)).append("','1002','1001','");
                        sb.append(zonegb).append("00000000',1,0,NOW(),NOW());");
                    } else if (zonegb.length()==4){
                        tsZone.setZoneGb(Long.valueOf(zonegb+"000000"));
                        tsZone.setZoneCoungb(Long.valueOf(zonegb+"000000"));
                        tsZone.setZoneLevCode("1003");
                        sb.append("'").append(zonegb).append("000000','").append(zonename).append("','").append(ToFirstChar(zonename)).append("','1003','1001','");
                        sb.append(zonegb).append("000000',1,0,NOW(),NOW());");
                    } else if(zonegb.length()==8){
                        if(zonegb.endsWith("00")){
                            tsZone.setZoneGb(Long.valueOf(zonegb+"00"));
                            tsZone.setZoneCoungb(Long.valueOf(zonegb+"00"));
                            tsZone.setZoneLevCode("1004");
                            sb.append("'").append(zonegb).append("00','").append(zonename).append("','").append(ToFirstChar(zonename)).append("','1004','1001','");
                            sb.append(zonegb).append("00',1,0,NOW(),NOW());");
                        } else {
                            tsZone.setZoneGb(Long.valueOf(zonegb+"00"));
                            tsZone.setZoneCoungb(Long.valueOf(zonegb+"00"));
                            tsZone.setZoneLevCode("1005");
                            sb.append("'").append(zonegb).append("00','").append(zonename).append("','").append(ToFirstChar(zonename)).append("','1005','1001','");
                            sb.append(zonegb).append("00',1,0,NOW(),NOW());");
                        }

                    } else {
                        System.out.println(1);
                    }
                    tsZone.setZoneName(zonename);
                    tsZone.setSplsht(ToFirstChar(zonename));
                    tsZone.setZoneTypeCode("1001");
                    list.add(tsZone);
                    System.out.println(sb.toString());


                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nextline == null) break;
        }

    }

    public static String ToFirstChar(String chinese) {
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
}
